--[[
* MapLib 2.0
* File: <AddOn>\MapLib\MapLib_DropDown.lua
* $Revision: 152 $
* $Date: 2007-01-24 14:29:42 +0000 (Wed, 24 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

local vmajor, vminor = "MapLib-2.0", tonumber(string.sub("$Revision: 152 $", 12, -3))

-- Version check: Only one major instance of MapLib can be installed, if another already is: abort.
if (MapLib and MapLib.DropDown and (MapLib.DropDown.vmajor ~= vmajor or MapLib.DropDown.vminor >= vminor)) then return; end;

if (not MapLib) then MapLib = {}; end;
if (not MapLib.DropDown) then MapLib.DropDown = {}; end;
MapLib.DropDown.vmajor = vmajor;
MapLib.DropDown.vminor = vminor;
if (not MapLib.DropDown.SubMenu) then MapLib.DropDown.SubMenu = {}; end;

function MapLib.DropDown.SubMenu.OnClick()
	MapLibSaved.poiDropDown[MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].nameID][MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].submenu[this:GetID()].style] = not MapLibSaved.poiDropDown[MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].nameID][MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].submenu[this:GetID()].style];
	this:SetChecked(MapLibSaved.poiDropDown[MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].nameID][MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].submenu[this:GetID()].style])
	MapLib.UpdatePOI("static");
end;

MapLib.DropDown.SubMenu.OnEnter = function()
	local text = MapLib.poiDropDown[MapLibDropDownSubMenuList.shownID].submenu[this:GetID()].text;
	if (text) then
		MapLibTooltip:ClearLines();
		MapLibTooltip:SetOwner(this, "ANCHOR_RIGHT", -21, -3);
		MapLibTooltip:SetText(text);
		MapLibTooltip:Show();
	else
		MapLibTooltip:Hide();
	end;
end

MapLib.DropDown.SubMenu.OnLeave = function()
	MapLibTooltip:Hide();
end

MapLib.DropDown.SubMenu.Show = function(position, submenu)
	-- fill with data from submenu...
	-- submenu if present table with submenu[i].texture, submenu[i].name, submenu[i].style, if submenu[1] has a name then all are threaded like they would, if not then not, info the same identifyer as in getPOI
	local button, texture;
	for i=1, getn(submenu), 1 do
		if (MapLib.NUM_DROPDOWNSUBMENUBUTTONS < i) then MapLib.XML.CreateDropDownSubMenuButton() end;
		button = getglobal("MapLibDropDownSubMenuButton"..i);
		texture = getglobal("MapLibDropDownSubMenuButton"..i.."Texture");
		if (submenu[i].texture) then
			texture:SetTexture(submenu[i].texture.file);
			if (submenu[i].texture.tcoordMinX) then texture:SetTexCoord(submenu[i].texture.tcoordMinX, submenu[i].texture.tcoordMaxX, submenu[i].texture.tcoordMinY, submenu[i].texture.tcoordMaxY); else texture:SetTexCoord(0,1,0,1); end;
			if (submenu[i].texture.colorR) then texture:SetVertexColor(submenu[i].texture.colorR, submenu[i].texture.colorG, submenu[i].texture.colorB, submenu[i].texture.colorA); else texture:SetVertexColor(1,1,1,1); end;
			texture:Show();
		else
			texture:Hide();
		end;
		button:SetChecked(MapLibSaved.poiDropDown[MapLib.poiDropDown[position].nameID][submenu[i].style]);
		button:Show();
	end;
	for i=getn(submenu)+1, MapLib.NUM_DROPDOWNSUBMENUBUTTONS, 1 do
		getglobal("MapLibDropDownSubMenuButton"..i):Hide();
	end;
	-- set height:
	MapLibDropDownSubMenuList:SetHeight( 19 + 25 * (floor((getn(submenu)-1)/5) + 1) );
	-- anchor and show
	MapLibDropDownSubMenuList:SetPoint("TOPLEFT", "MapLibDropDownButton"..position, "TOPRIGHT", 3, 8);
	MapLibDropDownSubMenuList.shownID = position;
	MapLibDropDownSubMenuList:Show();
end;

MapLib.DropDown.SubMenu.Hide = function(exception)
	if (MapLibDropDownSubMenuList.shownID ~= exception) then
		MapLibDropDownSubMenuList:Hide();
	end;
end;

MapLib.DropDown.OnClick = function()
	-- Always hide submenu:
	MapLib.DropDown.SubMenu.Hide();
	-- If Menu is set to the clicked button: hide it
	-- else generate and show it.
	if (MapLibDropDownList.shown == this:GetParent():GetName()) then
		MapLib.DropDown.Close();
	else
		MapLib.CloseMenus("DropDown");
		-- fill with the right info...
		if (this:GetParent():GetName() == "MapLibContinentDropDown") then
			for i=1, getn(MapLib.continents), 1 do
				if (i>MapLib.NUM_DROPDOWNBUTTONS) then MapLib.XML.CreateDropDownMenuButton(); end;
				getglobal("MapLibDropDownButton"..i.."Icon"):Hide();
				getglobal("MapLibDropDownButton"..i.."ExpandArrow"):Hide();
				getglobal("MapLibDropDownButton"..i.."Text"):SetPoint("LEFT", "MapLibDropDownButton"..i.."Check", "RIGHT", 0, 0);
				getglobal("MapLibDropDownButton"..i.."Text"):SetText(MapLib.continents[i]);
				getglobal("MapLibDropDownButton"..i):Show();
				local curCont = MapLib.GetShownContinentID();
				if ( curCont and MapLib.mapInfo[curCont].mapName == MapLib.continents[i]) then
					getglobal("MapLibDropDownButton"..i.."Check"):Show();
				else
					getglobal("MapLibDropDownButton"..i.."Check"):Hide();
				end;
			end;
			for i=getn(MapLib.continents)+1, MapLib.NUM_DROPDOWNBUTTONS, 1 do
				getglobal("MapLibDropDownButton"..i):Hide();
			end;
			MapLibDropDownList:SetHeight(16 * getn(MapLib.continents) + 28);
		elseif (this:GetParent():GetName() == "MapLibZoneDropDown") then
			local curCont = MapLib.GetShownContinentID();
			if (not curCont and not MapLib.zones[curCont]) then MapLib.DropDown.Close(); return; end;
			for i=1, getn(MapLib.zones[curCont]), 1 do
				if (i>MapLib.NUM_DROPDOWNBUTTONS) then MapLib.XML.CreateDropDownMenuButton(); end;
				getglobal("MapLibDropDownButton"..i.."Icon"):Hide();
				getglobal("MapLibDropDownButton"..i.."ExpandArrow"):Hide();
				getglobal("MapLibDropDownButton"..i.."Text"):SetPoint("LEFT", "MapLibDropDownButton"..i.."Check", "RIGHT", 0, 0);
				getglobal("MapLibDropDownButton"..i.."Text"):SetText(MapLib.zones[curCont][i]);
				getglobal("MapLibDropDownButton"..i):Show();
				if ( curCont and MapLib.mapInfo[MapLib.mapShown].mapName == MapLib.zones[curCont][i]) then
					getglobal("MapLibDropDownButton"..i.."Check"):Show();
				else
					getglobal("MapLibDropDownButton"..i.."Check"):Hide();
				end;
			end;
			for i=getn(MapLib.zones[curCont])+1, MapLib.NUM_DROPDOWNBUTTONS, 1 do
				getglobal("MapLibDropDownButton"..i):Hide();
			end;
			MapLibDropDownList:SetHeight(16 * getn(MapLib.zones[curCont]) + 28);
		elseif (this:GetParent():GetName() == "MapLibInstanceDropDown") then
			local curCont = MapLib.GetShownContinentID();
			if (not curCont or not MapLib.instances[curCont]) then MapLib.DropDown.Close(); return; end;
			for i=1, getn(MapLib.instances[curCont]), 1 do
				if (i>MapLib.NUM_DROPDOWNBUTTONS) then MapLib.XML.CreateDropDownMenuButton(); end;
				getglobal("MapLibDropDownButton"..i.."Icon"):Show();
				local mapType = MapLib.mapInfo[MapLib.GetMapIDByName(MapLib.instances[curCont][i])].mapType;
				getglobal("MapLibDropDownButton"..i.."Icon"):SetTexture(MapLib.PATH.."\\Icons\\"..mapType);
				getglobal("MapLibDropDownButton"..i.."ExpandArrow"):Hide();
				getglobal("MapLibDropDownButton"..i.."Text"):SetPoint("LEFT", "MapLibDropDownButton"..i.."Icon", "RIGHT", 1, 0);
				getglobal("MapLibDropDownButton"..i.."Text"):SetText(MapLib.instances[curCont][i]);
				getglobal("MapLibDropDownButton"..i):Show();
				if ( curCont and MapLib.mapInfo[MapLib.mapShown].mapName == MapLib.instances[curCont][i]) then
					getglobal("MapLibDropDownButton"..i.."Check"):Show();
				else
					getglobal("MapLibDropDownButton"..i.."Check"):Hide();
				end;
			end;
			for i=getn(MapLib.instances[curCont])+1, MapLib.NUM_DROPDOWNBUTTONS, 1 do
				getglobal("MapLibDropDownButton"..i):Hide();
			end;
			MapLibDropDownList:SetHeight(16 * getn(MapLib.instances[curCont]) + 28);
		elseif (this:GetParent():GetName() == "MapLibPOIDropDown") then
			-- build the POI menu
			for i=1, getn(MapLib.poiDropDown), 1 do
				if (i>MapLib.NUM_DROPDOWNBUTTONS) then MapLib.XML.CreateDropDownMenuButton(); end;
				getglobal("MapLibDropDownButton"..i.."Icon"):Hide();
				if (MapLib.poiDropDown[i].submenu) then 
					getglobal("MapLibDropDownButton"..i.."ExpandArrow"):Show();
				else
					getglobal("MapLibDropDownButton"..i.."ExpandArrow"):Hide();
				end;
				getglobal("MapLibDropDownButton"..i.."Text"):SetPoint("LEFT", "MapLibDropDownButton"..i.."Check", "RIGHT", 0, 0);
				getglobal("MapLibDropDownButton"..i.."Text"):SetText(MapLib.poiDropDown[i].name);
				getglobal("MapLibDropDownButton"..i):Show();
				if ( MapLibSaved.poiDropDown[MapLib.poiDropDown[i].nameID].isActivated ) then
					getglobal("MapLibDropDownButton"..i.."Check"):Show();
				else
					getglobal("MapLibDropDownButton"..i.."Check"):Hide();
				end;
			end;
			for i=getn(MapLib.poiDropDown)+1, MapLib.NUM_DROPDOWNBUTTONS, 1 do
				getglobal("MapLibDropDownButton"..i):Hide();
			end;
			MapLibDropDownList:SetHeight(16 * getn(MapLib.poiDropDown) + 28);
		end
		-- anchor and show
		MapLibDropDownList:SetPoint("TOPLEFT", this:GetParent(), "BOTTOMLEFT", 8, 7);
		MapLibDropDownList.shown = this:GetParent():GetName();
		MapLibDropDownList:Show();
	end
end

MapLib.DropDown.Close = function()
		MapLibDropDownList.shown = nil;
		MapLibDropDownList:Hide();
		MapLib.DropDown.SubMenu.Hide();
end;

MapLib.DropDown.MenuOnClick = function()
	if (MapLibDropDownList.shown == "MapLibContinentDropDown" or MapLibDropDownList.shown == "MapLibZoneDropDown" or MapLibDropDownList.shown == "MapLibInstanceDropDown") then
		MapLib.SetMapToID(MapLib.GetMapIDByName(getglobal(this:GetName().."Text"):GetText()));
	else
		if (MapLibSaved.poiDropDown[MapLib.poiDropDown[this:GetID()].nameID].isActivated) then 
			MapLibSaved.poiDropDown[MapLib.poiDropDown[this:GetID()].nameID].isActivated = false;
			getglobal("MapLibDropDownButton"..this:GetID().."Check"):Hide();
		else
			MapLibSaved.poiDropDown[MapLib.poiDropDown[this:GetID()].nameID].isActivated = true;
			getglobal("MapLibDropDownButton"..this:GetID().."Check"):Show();
		end;
		MapLib.UpdatePOI("static");
	end;
end

MapLib.DropDown.MenuOnEnter = function()
	getglobal(this:GetName().."Highlight"):Show();
	MapLib.DropDown.SubMenu.Hide(this:GetID());
end

MapLib.DropDown.MenuOnLeave = function()
	getglobal(this:GetName().."Highlight"):Hide();
end

MapLib.DropDown.ExpandArrowOnEnter = function()
	getglobal(this:GetParent():GetName().."Highlight"):Show();
	local id = this:GetParent():GetID();
	MapLib.DropDown.SubMenu.Show(id, MapLib.poiDropDown[id].submenu);
end

MapLib.DropDown.ExpandArrowOnLeave = function()
	getglobal(this:GetParent():GetName().."Highlight"):Hide();
	MapLibTooltip:Hide();
end

MapLib.DropDown.ExpandArrowOnClick = function()
end